﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.WHS2011
{
    public partial class AboutTabControl : UserControl
    {
        private static readonly LogFacade log = LogFacade.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public AboutTabControl()
        {
            InitializeComponent();
        }

        private ServiioConnection conn = null;
        //private IConsoleServices consoleServices = null;

        public void Initialize(ServiioConnection connection/*, IConsoleServices consoleServices*/)
        {
            try
            {
                if (this.conn != null)
                    return;

                log.Info("Initializing...");

                labelBuildNumber.Text += System.Reflection.Assembly.GetCallingAssembly().GetName().Version.ToString();

                this.conn = connection;
                //this.consoleServices = consoleServices;

                labelServiioVersion.Text += conn.Version;
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void linkLabelGeneric_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if (sender is LinkLabel)
                {
                    Uri uri = new Uri(((LinkLabel)sender).Tag.ToString());
                    Microsoft.WindowsServerSolutions.Administration.ObjectModel.ProcessUtilities.OpenUrl(uri);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void linkLabelUninstall_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                linkLabelUninstall.Enabled = false;
                // Can't do much with this because it is a funky process. Just start it and keep your fingers crosssed;
                User32Helper.StartChildProcess(this.ParentForm, conn.ServiceFilePath + "\\..\\uninstall.exe", "", false, false, false);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                linkLabelUninstall.Enabled = true;
            }
        }

        private void AboutTabControl_Resize(object sender, EventArgs e)
        {
            panelCenter.Left = this.Width / 2 - panelCenter.Width / 2;
        }

        private void AboutTabControl_Paint(object sender, PaintEventArgs e)
        {
            GuiHelper.DrawBorder(e, this, new GuiHelper.BorderSides(true, false, false, false));
        }

    }
}
